| Conditions | 1 |
| Paths | > 20000 |
| Total Lines | 412 |
| Code Lines | 216 |
| Lines | 20 |
| Ratio | 4.85 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /*! |
||
| 151 | function () { |
||
| 152 | function Dropdown(element, config) { |
||
| 153 | this._element = element; |
||
| 154 | this._popper = null; |
||
| 155 | this._config = this._getConfig(config); |
||
| 156 | this._menu = this._getMenuElement(); |
||
| 157 | this._inNavbar = this._detectNavbar(); |
||
| 158 | |||
| 159 | this._addEventListeners(); |
||
| 160 | } // Getters |
||
| 161 | |||
| 162 | |||
| 163 | var _proto = Dropdown.prototype; |
||
| 164 | |||
| 165 | // Public |
||
| 166 | _proto.toggle = function toggle() { |
||
| 167 | if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | var parent = Dropdown._getParentFromElement(this._element); |
||
| 172 | |||
| 173 | var isActive = $(this._menu).hasClass(ClassName.SHOW); |
||
| 174 | |||
| 175 | Dropdown._clearMenus(); |
||
| 176 | |||
| 177 | if (isActive) { |
||
| 178 | return; |
||
| 179 | } |
||
| 180 | |||
| 181 | var relatedTarget = { |
||
| 182 | relatedTarget: this._element |
||
| 183 | }; |
||
| 184 | var showEvent = $.Event(Event.SHOW, relatedTarget); |
||
| 185 | $(parent).trigger(showEvent); |
||
| 186 | |||
| 187 | if (showEvent.isDefaultPrevented()) { |
||
| 188 | return; |
||
| 189 | } // Disable totally Popper.js for Dropdown in Navbar |
||
| 190 | |||
| 191 | |||
| 192 | if (!this._inNavbar) { |
||
| 193 | /** |
||
| 194 | * Check for Popper dependency |
||
| 195 | * Popper - https://popper.js.org |
||
| 196 | */ |
||
| 197 | if (typeof Popper === 'undefined') { |
||
| 198 | throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org/)'); |
||
| 199 | } |
||
| 200 | |||
| 201 | var referenceElement = this._element; |
||
| 202 | |||
| 203 | if (this._config.reference === 'parent') { |
||
| 204 | referenceElement = parent; |
||
| 205 | } else if (Util.isElement(this._config.reference)) { |
||
| 206 | referenceElement = this._config.reference; // Check if it's jQuery element |
||
| 207 | |||
| 208 | if (typeof this._config.reference.jquery !== 'undefined') { |
||
| 209 | referenceElement = this._config.reference[0]; |
||
| 210 | } |
||
| 211 | } // If boundary is not `scrollParent`, then set position to `static` |
||
| 212 | // to allow the menu to "escape" the scroll parent's boundaries |
||
| 213 | // https://github.com/twbs/bootstrap/issues/24251 |
||
| 214 | |||
| 215 | |||
| 216 | if (this._config.boundary !== 'scrollParent') { |
||
| 217 | $(parent).addClass(ClassName.POSITION_STATIC); |
||
| 218 | } |
||
| 219 | |||
| 220 | this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig()); |
||
| 221 | } // If this is a touch-enabled device we add extra |
||
| 222 | // empty mouseover listeners to the body's immediate children; |
||
| 223 | // only needed because of broken event delegation on iOS |
||
| 224 | // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html |
||
| 225 | |||
| 226 | |||
| 227 | if ('ontouchstart' in document.documentElement && $(parent).closest(Selector.NAVBAR_NAV).length === 0) { |
||
| 228 | $(document.body).children().on('mouseover', null, $.noop); |
||
| 229 | } |
||
| 230 | |||
| 231 | this._element.focus(); |
||
| 232 | |||
| 233 | this._element.setAttribute('aria-expanded', true); |
||
| 234 | |||
| 235 | $(this._menu).toggleClass(ClassName.SHOW); |
||
| 236 | $(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.SHOWN, relatedTarget)); |
||
| 237 | }; |
||
| 238 | |||
| 239 | _proto.show = function show() { |
||
| 240 | if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED) || $(this._menu).hasClass(ClassName.SHOW)) { |
||
| 241 | return; |
||
| 242 | } |
||
| 243 | |||
| 244 | var relatedTarget = { |
||
| 245 | relatedTarget: this._element |
||
| 246 | }; |
||
| 247 | var showEvent = $.Event(Event.SHOW, relatedTarget); |
||
| 248 | |||
| 249 | var parent = Dropdown._getParentFromElement(this._element); |
||
| 250 | |||
| 251 | $(parent).trigger(showEvent); |
||
| 252 | |||
| 253 | if (showEvent.isDefaultPrevented()) { |
||
| 254 | return; |
||
| 255 | } |
||
| 256 | |||
| 257 | $(this._menu).toggleClass(ClassName.SHOW); |
||
| 258 | $(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.SHOWN, relatedTarget)); |
||
| 259 | }; |
||
| 260 | |||
| 261 | _proto.hide = function hide() { |
||
| 262 | if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED) || !$(this._menu).hasClass(ClassName.SHOW)) { |
||
| 263 | return; |
||
| 264 | } |
||
| 265 | |||
| 266 | var relatedTarget = { |
||
| 267 | relatedTarget: this._element |
||
| 268 | }; |
||
| 269 | var hideEvent = $.Event(Event.HIDE, relatedTarget); |
||
| 270 | |||
| 271 | var parent = Dropdown._getParentFromElement(this._element); |
||
| 272 | |||
| 273 | $(parent).trigger(hideEvent); |
||
| 274 | |||
| 275 | if (hideEvent.isDefaultPrevented()) { |
||
| 276 | return; |
||
| 277 | } |
||
| 278 | |||
| 279 | $(this._menu).toggleClass(ClassName.SHOW); |
||
| 280 | $(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.HIDDEN, relatedTarget)); |
||
| 281 | }; |
||
| 282 | |||
| 283 | _proto.dispose = function dispose() { |
||
| 284 | $.removeData(this._element, DATA_KEY); |
||
| 285 | $(this._element).off(EVENT_KEY); |
||
| 286 | this._element = null; |
||
| 287 | this._menu = null; |
||
| 288 | |||
| 289 | if (this._popper !== null) { |
||
| 290 | this._popper.destroy(); |
||
| 291 | |||
| 292 | this._popper = null; |
||
| 293 | } |
||
| 294 | }; |
||
| 295 | |||
| 296 | _proto.update = function update() { |
||
| 297 | this._inNavbar = this._detectNavbar(); |
||
| 298 | |||
| 299 | if (this._popper !== null) { |
||
| 300 | this._popper.scheduleUpdate(); |
||
| 301 | } |
||
| 302 | } // Private |
||
| 303 | ; |
||
| 304 | |||
| 305 | _proto._addEventListeners = function _addEventListeners() { |
||
| 306 | var _this = this; |
||
| 307 | |||
| 308 | $(this._element).on(Event.CLICK, function (event) { |
||
| 309 | event.preventDefault(); |
||
| 310 | event.stopPropagation(); |
||
| 311 | |||
| 312 | _this.toggle(); |
||
| 313 | }); |
||
| 314 | }; |
||
| 315 | |||
| 316 | _proto._getConfig = function _getConfig(config) { |
||
| 317 | config = _objectSpread({}, this.constructor.Default, $(this._element).data(), config); |
||
| 318 | Util.typeCheckConfig(NAME, config, this.constructor.DefaultType); |
||
| 319 | return config; |
||
| 320 | }; |
||
| 321 | |||
| 322 | _proto._getMenuElement = function _getMenuElement() { |
||
| 323 | if (!this._menu) { |
||
| 324 | var parent = Dropdown._getParentFromElement(this._element); |
||
| 325 | |||
| 326 | if (parent) { |
||
| 327 | this._menu = parent.querySelector(Selector.MENU); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | return this._menu; |
||
| 332 | }; |
||
| 333 | |||
| 334 | _proto._getPlacement = function _getPlacement() { |
||
| 335 | var $parentDropdown = $(this._element.parentNode); |
||
| 336 | var placement = AttachmentMap.BOTTOM; // Handle dropup |
||
| 337 | |||
| 338 | if ($parentDropdown.hasClass(ClassName.DROPUP)) { |
||
| 339 | placement = AttachmentMap.TOP; |
||
| 340 | |||
| 341 | if ($(this._menu).hasClass(ClassName.MENURIGHT)) { |
||
| 342 | placement = AttachmentMap.TOPEND; |
||
| 343 | } |
||
| 344 | } else if ($parentDropdown.hasClass(ClassName.DROPRIGHT)) { |
||
| 345 | placement = AttachmentMap.RIGHT; |
||
| 346 | } else if ($parentDropdown.hasClass(ClassName.DROPLEFT)) { |
||
| 347 | placement = AttachmentMap.LEFT; |
||
| 348 | } else if ($(this._menu).hasClass(ClassName.MENURIGHT)) { |
||
| 349 | placement = AttachmentMap.BOTTOMEND; |
||
| 350 | } |
||
| 351 | |||
| 352 | return placement; |
||
| 353 | }; |
||
| 354 | |||
| 355 | _proto._detectNavbar = function _detectNavbar() { |
||
| 356 | return $(this._element).closest('.navbar').length > 0; |
||
| 357 | }; |
||
| 358 | |||
| 359 | _proto._getOffset = function _getOffset() { |
||
| 360 | var _this2 = this; |
||
| 361 | |||
| 362 | var offset = {}; |
||
| 363 | |||
| 364 | if (typeof this._config.offset === 'function') { |
||
| 365 | offset.fn = function (data) { |
||
| 366 | data.offsets = _objectSpread({}, data.offsets, _this2._config.offset(data.offsets, _this2._element) || {}); |
||
| 367 | return data; |
||
| 368 | }; |
||
| 369 | } else { |
||
| 370 | offset.offset = this._config.offset; |
||
| 371 | } |
||
| 372 | |||
| 373 | return offset; |
||
| 374 | }; |
||
| 375 | |||
| 376 | _proto._getPopperConfig = function _getPopperConfig() { |
||
| 377 | var popperConfig = { |
||
| 378 | placement: this._getPlacement(), |
||
| 379 | modifiers: { |
||
| 380 | offset: this._getOffset(), |
||
| 381 | flip: { |
||
| 382 | enabled: this._config.flip |
||
| 383 | }, |
||
| 384 | preventOverflow: { |
||
| 385 | boundariesElement: this._config.boundary |
||
| 386 | } |
||
| 387 | } // Disable Popper.js if we have a static display |
||
| 388 | |||
| 389 | }; |
||
| 390 | |||
| 391 | if (this._config.display === 'static') { |
||
| 392 | popperConfig.modifiers.applyStyle = { |
||
| 393 | enabled: false |
||
| 394 | }; |
||
| 395 | } |
||
| 396 | |||
| 397 | return popperConfig; |
||
| 398 | } // Static |
||
| 399 | ; |
||
| 400 | |||
| 401 | View Code Duplication | Dropdown._jQueryInterface = function _jQueryInterface(config) { |
|
| 402 | return this.each(function () { |
||
| 403 | var data = $(this).data(DATA_KEY); |
||
| 404 | |||
| 405 | var _config = typeof config === 'object' ? config : null; |
||
| 406 | |||
| 407 | if (!data) { |
||
| 408 | data = new Dropdown(this, _config); |
||
| 409 | $(this).data(DATA_KEY, data); |
||
| 410 | } |
||
| 411 | |||
| 412 | if (typeof config === 'string') { |
||
| 413 | if (typeof data[config] === 'undefined') { |
||
| 414 | throw new TypeError("No method named \"" + config + "\""); |
||
| 415 | } |
||
| 416 | |||
| 417 | data[config](); |
||
| 418 | } |
||
| 419 | }); |
||
| 420 | }; |
||
| 421 | |||
| 422 | Dropdown._clearMenus = function _clearMenus(event) { |
||
| 423 | if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH || event.type === 'keyup' && event.which !== TAB_KEYCODE)) { |
||
| 424 | return; |
||
| 425 | } |
||
| 426 | |||
| 427 | var toggles = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE)); |
||
| 428 | |||
| 429 | for (var i = 0, len = toggles.length; i < len; i++) { |
||
| 430 | var parent = Dropdown._getParentFromElement(toggles[i]); |
||
| 431 | |||
| 432 | var context = $(toggles[i]).data(DATA_KEY); |
||
| 433 | var relatedTarget = { |
||
| 434 | relatedTarget: toggles[i] |
||
| 435 | }; |
||
| 436 | |||
| 437 | if (event && event.type === 'click') { |
||
| 438 | relatedTarget.clickEvent = event; |
||
| 439 | } |
||
| 440 | |||
| 441 | if (!context) { |
||
| 442 | continue; |
||
| 443 | } |
||
| 444 | |||
| 445 | var dropdownMenu = context._menu; |
||
| 446 | |||
| 447 | if (!$(parent).hasClass(ClassName.SHOW)) { |
||
| 448 | continue; |
||
| 449 | } |
||
| 450 | |||
| 451 | if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) && $.contains(parent, event.target)) { |
||
| 452 | continue; |
||
| 453 | } |
||
| 454 | |||
| 455 | var hideEvent = $.Event(Event.HIDE, relatedTarget); |
||
| 456 | $(parent).trigger(hideEvent); |
||
| 457 | |||
| 458 | if (hideEvent.isDefaultPrevented()) { |
||
| 459 | continue; |
||
| 460 | } // If this is a touch-enabled device we remove the extra |
||
| 461 | // empty mouseover listeners we added for iOS support |
||
| 462 | |||
| 463 | |||
| 464 | if ('ontouchstart' in document.documentElement) { |
||
| 465 | $(document.body).children().off('mouseover', null, $.noop); |
||
| 466 | } |
||
| 467 | |||
| 468 | toggles[i].setAttribute('aria-expanded', 'false'); |
||
| 469 | $(dropdownMenu).removeClass(ClassName.SHOW); |
||
| 470 | $(parent).removeClass(ClassName.SHOW).trigger($.Event(Event.HIDDEN, relatedTarget)); |
||
| 471 | } |
||
| 472 | }; |
||
| 473 | |||
| 474 | Dropdown._getParentFromElement = function _getParentFromElement(element) { |
||
| 475 | var parent; |
||
| 476 | var selector = Util.getSelectorFromElement(element); |
||
| 477 | |||
| 478 | if (selector) { |
||
| 479 | parent = document.querySelector(selector); |
||
| 480 | } |
||
| 481 | |||
| 482 | return parent || element.parentNode; |
||
| 483 | } // eslint-disable-next-line complexity |
||
| 484 | ; |
||
| 485 | |||
| 486 | Dropdown._dataApiKeydownHandler = function _dataApiKeydownHandler(event) { |
||
| 487 | // If not input/textarea: |
||
| 488 | // - And not a key in REGEXP_KEYDOWN => not a dropdown command |
||
| 489 | // If input/textarea: |
||
| 490 | // - If space key => not a dropdown command |
||
| 491 | // - If key is other than escape |
||
| 492 | // - If key is not up or down => not a dropdown command |
||
| 493 | // - If trigger inside the menu => not a dropdown command |
||
| 494 | if (/input|textarea/i.test(event.target.tagName) ? event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE && (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE || $(event.target).closest(Selector.MENU).length) : !REGEXP_KEYDOWN.test(event.which)) { |
||
| 495 | return; |
||
| 496 | } |
||
| 497 | |||
| 498 | event.preventDefault(); |
||
| 499 | event.stopPropagation(); |
||
| 500 | |||
| 501 | if (this.disabled || $(this).hasClass(ClassName.DISABLED)) { |
||
| 502 | return; |
||
| 503 | } |
||
| 504 | |||
| 505 | var parent = Dropdown._getParentFromElement(this); |
||
| 506 | |||
| 507 | var isActive = $(parent).hasClass(ClassName.SHOW); |
||
| 508 | |||
| 509 | if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) { |
||
| 510 | if (event.which === ESCAPE_KEYCODE) { |
||
| 511 | var toggle = parent.querySelector(Selector.DATA_TOGGLE); |
||
| 512 | $(toggle).trigger('focus'); |
||
| 513 | } |
||
| 514 | |||
| 515 | $(this).trigger('click'); |
||
| 516 | return; |
||
| 517 | } |
||
| 518 | |||
| 519 | var items = [].slice.call(parent.querySelectorAll(Selector.VISIBLE_ITEMS)); |
||
| 520 | |||
| 521 | if (items.length === 0) { |
||
| 522 | return; |
||
| 523 | } |
||
| 524 | |||
| 525 | var index = items.indexOf(event.target); |
||
| 526 | |||
| 527 | if (event.which === ARROW_UP_KEYCODE && index > 0) { |
||
| 528 | // Up |
||
| 529 | index--; |
||
| 530 | } |
||
| 531 | |||
| 532 | if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { |
||
| 533 | // Down |
||
| 534 | index++; |
||
| 535 | } |
||
| 536 | |||
| 537 | if (index < 0) { |
||
| 538 | index = 0; |
||
| 539 | } |
||
| 540 | |||
| 541 | items[index].focus(); |
||
| 542 | }; |
||
| 543 | |||
| 544 | _createClass(Dropdown, null, [{ |
||
| 545 | key: "VERSION", |
||
| 546 | get: function get() { |
||
| 547 | return VERSION; |
||
| 548 | } |
||
| 549 | }, { |
||
| 550 | key: "Default", |
||
| 551 | get: function get() { |
||
| 552 | return Default; |
||
| 553 | } |
||
| 554 | }, { |
||
| 555 | key: "DefaultType", |
||
| 556 | get: function get() { |
||
| 557 | return DefaultType; |
||
| 558 | } |
||
| 559 | }]); |
||
| 560 | |||
| 561 | return Dropdown; |
||
| 562 | }(); |
||
| 563 | /** |
||
| 596 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.